
package gd.ip2region;

import cn.clazz.IP;
import cn.util.Print;
import cn.util.Roll;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbMakerConfigException;
import org.lionsoul.ip2region.DbSearcher;
import org.nutz.ioc.loader.annotation.IocBean;

/**
 * IP2Region的包装类
 * http://git.oschina.net/lionsoul/ip2region
 *
 * @date 2017.08.06
 */
@IocBean
public class SourceIP2Region
{

	final private static Logger log = LogManager.getLogger ( SourceIP2Region.class );

	private static DbConfig dbConfig;

	private static DbSearcher dbSearcher;

	/**
	 *
	 * @param argv
	 */
	public static void main ( String[] argv )
	  throws UnsupportedEncodingException ,
				DbMakerConfigException ,
				FileNotFoundException ,
				IOException
	{
//		URL url = SourceIP2Region.class.getResource ( "ip2region-2017-10-09.db" );
//			String path = url.getFile ();

		Print.print ( System.getProperties () );
		System.out.println ( System.getProperty ( "java.io.tmpdir" ) );

		SourceIP2Region sip2r = new SourceIP2Region ();
		Roll roll = new Roll ();

		for ( int i = 0 ; i < 10 ; i ++ )
		{
			String ip = roll.rIPV4 ();

			System.out.println ( ip );
			System.out.println ( sip2r.search2CountryCity ( ip ) );
			System.out.println ( "" );
		}

	}

	public void init ()
	  throws DbMakerConfigException ,
				FileNotFoundException ,
				UnsupportedEncodingException
	{
		String nameDB = "ip2region-2017-10-09.db";
		String pathDB = System.getProperty ( "java.io.tmpdir" ) + File.pathSeparator + nameDB;

		File fileDB = new File ( pathDB );
		if (  ! fileDB.exists () )
		{
			byte[] buffer = new byte[ 456 ];

			try ( InputStream is = SourceIP2Region.class.getResourceAsStream ( nameDB ) ;
					BufferedInputStream bis = new BufferedInputStream ( is ) ;

					FileOutputStream fos = new FileOutputStream ( fileDB ) ;
					BufferedOutputStream bos = new BufferedOutputStream ( fos ); )
			{

				fileDB.createNewFile ();

				while ( bis.read ( buffer ) != -1 )
				{
					bos.write ( buffer );
				}

			}
			catch ( IOException ex )
			{
				ex.printStackTrace ();
				System.err.println ( ex );
			}
		}

		if ( dbConfig == null )
		{
			dbConfig = new DbConfig ();
		}

		if ( dbSearcher == null )
		{
			dbSearcher = new DbSearcher ( dbConfig , pathDB );

		}
	}

	/**
	 * @param ip
	 *
	 * @return
	 *
	 * @date 2017.08.06
	 */
	public String local ( String ip )
	{
		if ( ip.contains ( ":" ) )
		{
			ip = ip.split ( ":" )[ 0 ];
		}

		return search ( ip ).getRegion ();

	}

	/**
	 * 根据IP返回地区信息
	 *
	 * @param ip
	 * string IP
	 *
	 * @return
	 *
	 * @throws org.lionsoul.ip2region.DbMakerConfigException
	 * @throws java.io.IOException
	 */
	public DataBlock search ( String ip )
	{
		try
		{
			init ();

			DataBlock dataBlock = dbSearcher.btreeSearch ( ip );
			return dataBlock;
		}
		catch ( DbMakerConfigException
				  | IOException ex )
		{
			java.util.logging.Logger.getLogger ( SourceIP2Region.class.getName () ).log ( Level.SEVERE , null , ex );
			return null;
		}

	}

	/**
	 * 根据IP返回地区信息
	 *
	 * @param ip
	 * string IP
	 *
	 * @return
	 *
	 * @throws org.lionsoul.ip2region.DbMakerConfigException
	 * @throws java.io.IOException
	 */
	public DataBlock search ( IP ip ) 
	{
		return search ( ip.toString () );
	}

	/**
	 * 根据IP返回地区信息
	 *
	 * @param ip
	 * string IP
	 *
	 * @return
	 *
	 * @throws org.lionsoul.ip2region.DbMakerConfigException
	 * @throws java.io.IOException
	 */
	public String search2Country ( String ip )
	{
		return search2Country ( new IP ( ip ) );
	}

	public String search2CountryCity ( String ip )
	{
		DataBlock db = search ( ip );

		String[] datas = db.toString ().split ( "\\|" );

		return datas[ 1 ] + datas[ 4 ];
	}

	/**
	 * 根据IP返回地区信息
	 *
	 * @param ip
	 * string IP
	 *
	 * @return
	 *
	 * @throws org.lionsoul.ip2region.DbMakerConfigException
	 * @throws java.io.IOException
	 */
	public String search2Country ( IP ip )
	{
		DataBlock dataBlock = search ( ip );

		return dataBlock == null
				 ? "火星"
				 : dataBlock.getRegion ().split ( "\\|" )[ 0 ];
	}

}
